package com.smartHire.controller.User;

import com.smartHire.domain.User;
import com.smartHire.service.UserService;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.jws.soap.SOAPBinding;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet(name = "ChargeServlet", value = "/ChargeServlet")
public class ChargeServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        String open_id = request.getParameter("open_id");
        String money = request.getParameter("money");

        WebApplicationContext ctx = null;
        //使用框架中的方法，获取容器对象
        ServletContext sc = getServletContext();
        ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(sc);
        System.out.println("容器对象的信息========"+ctx);

        try {
            UserService userService = (UserService) ctx.getBean("userService");
            User user = new User();
            user.setU_Id(open_id);

            user = userService.queryUserById(user);

            user.setBalance(user.getBalance() + Float.parseFloat(money));

            if(userService.updateBalance(user)==1)
            {
                response.getWriter().write("OK");
            }else
            {
                response.getWriter().write("NO");
            }


        }catch (Exception e)
        {
            e.printStackTrace();
        }

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
